package com.op.lib;

import java.io.File;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.os.Environment;

public class OpJsHelper {

    private static String       sPackageName;

    private static String       sFileDirectory;

    private static String       sExternalFilesDir;

    private static String       sExternalStorageDir;

    private static JSActivity   sContext;

    private static AssetManager sAssetMgr;

    public OpJsHelper() {
    }

    public static void init(final JSActivity pContext) {
        sContext = pContext;
        final ApplicationInfo applicationInfo = pContext.getApplicationInfo();

        sAssetMgr = pContext.getAssets();
        sPackageName = applicationInfo.packageName;
        sFileDirectory = appendEnd(pContext.getFilesDir());
        sExternalFilesDir = appendEnd(pContext.getExternalFilesDir(null));
        sExternalStorageDir = appendEnd(Environment.getExternalStorageDirectory());

        //System.out.println("sPackageName:" + sPackageName);
        //System.out.println("sFileDirectory:" + sFileDirectory);
        //System.out.println("sExternalFilesDir:" + sExternalFilesDir);
        //System.out.println("sExternalStorageDir:" + sExternalStorageDir);
    }

    private static String appendEnd(File f) {
        if (f == null) {
            return "";
        }
        String str = f.getAbsolutePath();
        if (str.endsWith("/")) {
            return str;
        }
        return str + "/";
    }

    public static String getInternalFilesDir() {
        return sFileDirectory;
    }

    public static String getExternalFilesDir() {
        return sExternalFilesDir;
    }

    public static String getExternalStorageDir() {
        return sExternalStorageDir;
    }

    public static Object getAssetManager() {
        return sAssetMgr;
    }

    public static Object getContext() {
        return sContext;
    }

    private static Object runOperation(JSONObject obj) {
        try {
            return sContext.receiveMessage(obj);
        } catch (JSONException e) {
            return e.getMessage();
        }
    }

    /**
     * handle native message
     * @param message
     */
    public static String handleNative(String message) {
        try {
            Object json = new JSONTokener(message).nextValue();
            if (json == null) {
                return null;
            }
            if (json instanceof JSONObject) {
                Object r = runOperation((JSONObject) json);
                return r == null ? null : r.toString();
            } else if (json instanceof JSONArray) {
                JSONArray array = (JSONArray) json;
                JSONArray response = new JSONArray();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.optJSONObject(i);
                    if (obj != null) {
                        response.put(runOperation(obj));
                    } else {
                        response.put(null);
                    }
                }
                return response.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
